//
//  ZbTableViewCustomHeadView.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/30.
//  Copyright © 2016年 lanou.com. All rights reserved.


#import "ZbTableViewCustomHeadView.h"

#import "MoreInformationViewController.h"
#import "UIImageView+WebCache.h"

#import "ModelForListen.h"
#import "DataBaseHandler.h"

@interface ZbTableViewCustomHeadView ()

@property (nonatomic, retain) UILabel *labelForCount;

@property (nonatomic, assign) BOOL isYes;
@property (nonatomic, assign) BOOL isOk;

@end

@implementation ZbTableViewCustomHeadView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
    [self addSubview:view];
    view.backgroundColor = [UIColor whiteColor];

    self.buttonForCollect = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForCollect.frame  = CGRectMake(0, 1, [UIScreen mainScreen].bounds.size.width / 2, 60);
    self.buttonForCollect.titleLabel.font = [UIFont systemFontOfSize:20];
    [self addSubview:self.buttonForCollect];
//    [self.buttonForCollect setTitle:@"收藏" forState:UIControlStateNormal];
//    [self.buttonForCollect setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    
//    NSArray *arr = [[DataBaseHandler shareDataBaseHandler]selectForModel];
//    for (ModelForListen *model in arr) {
//        NSLog(@"11111111%@",model.title);
//        NSLog(@"22222222%@",self.model.title);
//        NSLog(@"%d",[self.model.title isEqualToString:model.title]);
//        
//        // 注:此处的self.model.title会一直为空.因为此处走的是该视图的初始化方法.值还没有传过来.
//    if ([self.model.title isEqualToString:model.title]) {
//    
//        [self.buttonForCollect setTitle:@"已收藏" forState:UIControlStateNormal];
//        [self.buttonForCollect setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    }
//    }
    
    [self.buttonForCollect addTarget:self action:@selector(handleCollectButton:) forControlEvents:UIControlEventTouchUpInside];
 
//
//    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    button1.frame  = CGRectMake([UIScreen mainScreen].bounds.size.width / 3, 1, [UIScreen mainScreen].bounds.size.width / 3, 60);
//    [button1 setTitle:@"批量下载" forState:UIControlStateNormal];
//    button1.titleLabel.font = [UIFont systemFontOfSize:20];
//    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [self addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame  = CGRectMake([UIScreen mainScreen].bounds.size.width * 1 / 2, 1, [UIScreen mainScreen].bounds.size.width / 2, 60);
    [button2 setTitle:@"相关推荐" forState:UIControlStateNormal];
    button2.titleLabel.font = [UIFont systemFontOfSize:20];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self addSubview:button2];
    [button2 addTarget:self action:@selector(handleAboutRecomment:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 61, [UIScreen mainScreen].bounds.size.width, 1)];
    [self addSubview:view2];
    view2.backgroundColor = [UIColor whiteColor];
    [view2 release];
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 62, [UIScreen mainScreen].bounds.size.width, 50)];
    [self addSubview:view3];
    view3.backgroundColor = [UIColor lightTextColor];
    
    self.labelForCount = [[UILabel alloc]initWithFrame:CGRectMake(20, 0,100,50)];
    self.labelForCount.text = @"共5集";
    [view3 addSubview:self.labelForCount];
    [self.labelForCount release];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 0, 100, 50)];
    label.text = @"选集";
    [view3 addSubview:label];
    [label release];
}



- (void)handleCollectButton:(UIButton *)button {

    NSLog(@"收藏");
   NSArray *arr = [[DataBaseHandler shareDataBaseHandler] selectForModel];
    self.isYes = 0;
    for (ModelForListen *model in arr) {
        NSLog(@"3333333%@",self.model.title);
        NSLog(@"444444%@",model.title);
        if ([model.title isEqualToString:self.model.title]) {
            self.isYes = 1;
        }
    }
    if (self.isYes) {
        
        [[DataBaseHandler shareDataBaseHandler]deleteWithModel:self.model];
        [button setTitle:@"收藏" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        
        
        
      [[DataBaseHandler shareDataBaseHandler]insertModel:self.model];
        [button setTitle:@"已收藏" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:24];
    }
}

- (void)handleAboutRecomment:(UIButton *)button {

    NSLog(@"相关推荐");
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;

    MoreInformationViewController *moreInformation = [[MoreInformationViewController alloc]init];
    
    [vc.navigationController pushViewController:moreInformation animated:YES];
    [vc.navigationController setNavigationBarHidden:NO];

}

- (void)passCountWithModel:(NSArray *)arrCount {

    NSString *str = [NSString stringWithFormat:@"共%lu集",arrCount.count];
    self.labelForCount.text = str;
}


@end
